package com.egao.orderB.service.impl;

import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.DateUtil;
import com.egao.common.core.damon.utils.StringUtil;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.BillFeeBiz;
import com.egao.orderB.entity.Bill;
import com.egao.orderB.entity.BillFee;
import com.egao.orderB.entity.BillFeeWriteOffItem;
import com.egao.orderB.service.BillFeeService;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//import com.egao.orderB.mapper.BillFeeMapper;
//import javax.transaction.Transactional;

/**
 * 订单运费服务实现类
 * @author : Doman
 * @date : 2020-06-15 16:11:54
 * @version ：1.0
 */
@Service
public class BillFeeServiceImpl /*extends ServiceImpl<BillFeeMapper, BillFee> */implements BillFeeService {

	@Autowired
	private BillFeeBiz billFeeBiz;

    /**
    @Override
    public PageResult<BillFee> listPage(PageParam<BillFee> page) {
        List<BillFee> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<BillFee> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JsonResult list(PageResult2<T> page, BillFee billFee, User loginUser)  throws Exception{
        return billFeeBiz.list(page,billFee,loginUser);
    }


    @Override
    public JsonResult listByFeeName(PageResult2<T> page, BillFee billFee, User loginUser)  throws Exception{
        return billFeeBiz.listByFeeName(page,billFee,loginUser);
    }

    @Override
    public JsonResult list(BillFee billFee, User loginUser) {
        return JsonResult.ok().setData(billFeeBiz.list(billFee,loginUser));
    }

    @Override
    public JsonResult chartTotalFee(BillFee billFee, User loginUser) {
//                "settlement :\n " +
//                "不传值: 总共的费用\n" +
//                "传值:1  未销账部分\n" +
//                "传值:2  已销账部分\n" +
        JSONObject obj = new JSONObject();
        billFee.setSettlement(null);
        Integer ret = billFeeBiz.chartTotalFee(billFee,loginUser);
        int a = ret==null?0:ret;
        List<BillFee> ret2 = billFeeBiz.chartTotalMonthFee(billFee,loginUser);
        obj.put("total",ret==null?"0":ret);;
        obj.put("totalList",ret2);;
        billFee.setSettlement("1");
        ret = billFeeBiz.chartTotalFee(billFee,loginUser);
        int b = ret==null?0:ret;
        ret2 = billFeeBiz.chartTotalMonthFee(billFee,loginUser);
        obj.put("unWriteOff",ret==null?"0":ret);;
        obj.put("unWriteOffList",ret2);;
        billFee.setSettlement("2");
        ret = billFeeBiz.chartTotalFee(billFee,loginUser);
        int c = ret==null?0:ret;
        ret2 = billFeeBiz.chartTotalMonthFee(billFee,loginUser);
        obj.put("writeOff",ret==null?"0":ret);
        obj.put("writeOffList",ret2);;
        obj.put("aa","[{name:'',value:"+a+",   itemStyle: {normal: {color: 'rgba(80,150,224,0)'}}},{name: '已结算',value: "+c+"},{name:'未结算',value:"+b+"}]");;
        return JsonResult.ok().setData(obj);
    }

    @Override
    public JsonResult exportList(BillFee billFee, User loginUser) {
        return JsonResult.ok().setData(billFeeBiz.exportList(billFee,loginUser));
    }

    @Override
    public List<String> feeNames(Bill bill, List<String> feeNames, User loginUser) {
        return billFeeBiz.feeNames(bill, feeNames, loginUser);
    }
    @Override
    public List<String> feeNamesBillAndApply(Bill bill, List<String> feeNames, User loginUser) {
        return billFeeBiz.feeNamesBillAndApply(bill, feeNames, loginUser);
    }

    @Override
    public BillFee getById(BillFee billFee) {
        return billFeeBiz.getById(billFee);
    }

    @Transactional
    @Override
    public JsonResult save(BillFee billFee, User loginUser) {
        if(StringUtil.isBlank(billFee.getBillId(),billFee.getFobject()
                ,billFee.getInvoiceid()
                ,billFee.getInvoicename(),billFee.getInvoicestate()
                ,billFee.getCurrency(),billFee.getExchangeRate())){
            return JsonResult.error("请填写完整内容,操作失败!");
        }
        if (billFeeBiz.save(billFee, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult saveBatch(List<BillFee> billFeeList, User loginUser) {
        for (BillFee billFee: billFeeList) {
            if(StringUtil.isBlank(billFee.getId(),billFee.getFobject())){
                return JsonResult.error("请填写完整内容,操作失败!");
            }
            billFeeBiz.save(billFee, loginUser);
        }
        return JsonResult.ok("添加成功");
    }

    @Transactional
    @Override
    public JsonResult update(BillFee billFee, User loginUser) {
        if(StringUtil.isBlank(billFee.getId(),billFee.getInvoiceid()
                ,billFee.getInvoicename(),billFee.getInvoicestate()
                ,billFee.getCurrency(),billFee.getExchangeRate()
        )){
            return JsonResult.error("请填写完整内容,操作失败!");
        }
        if (billFeeBiz.update(billFee, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult updateFeeStateByBill(BillFee billFee, User loginUser) {
        if(StringUtil.isBlank(billFee.getBillId())){
            return JsonResult.error("请填写完整内容,操作失败!");
        }
        billFee.setFeeState("已确认");
        billFee.setFeePerson(loginUser.getNickName());
        billFee.setFeeTime(DateUtil.getCurrentDate());
        if (billFeeBiz.updateFeeStateByBill(billFee, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Override
    public JsonResult updateFeeState(BillFee billFee, User loginUser) {
        if(Utils.isEmpty(billFee.getId())
        ){
            return JsonResult.error("请填写id,操作失败!");
        }
        if (billFeeBiz.updateFeeState(billFee, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Override
    public JsonResult remove(BillFee billFee, User loginUser) {
        if(Utils.isEmpty(billFee.getId())
        ){
            return JsonResult.error("请填写id,操作失败!");
        }
        if (billFeeBiz.remove(billFee, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult updateWriteOffAccounts(BillFeeWriteOffItem billFeeWriteOffItem, User loginUser) {
        if (billFeeBiz.updateWriteOffAccounts(billFeeWriteOffItem, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }
}
